package com.nantian.nfcm.bms.loan.web;

import com.nantian.nfcm.bms.loan.entity.MsgInfo;
import com.nantian.nfcm.bms.loan.service.MsgInfoService;
import com.nantian.nfcm.util.vo.GridData;
import com.nantian.nfcm.util.vo.LoginBean;
import com.nantian.nfcm.util.vo.ResultInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/msgInfo")
public class MsgInfoController {
    private static Logger log = LoggerFactory.getLogger(MsgInfoController.class);
    private MsgInfoService msgInfoService;

    @Autowired
    public MsgInfoController(MsgInfoService msgInfoService) {
        this.msgInfoService = msgInfoService;
    }

    @RequestMapping("/findByCondition")
    @ResponseBody
    private ResultInfo findByCondition(HttpServletRequest request){
        ResultInfo resultInfo = new ResultInfo();
        try {
            LoginBean loginUser = (LoginBean)request.getSession().getAttribute("loginInfo");
            GridData<MsgInfo> gridData = msgInfoService.findByCondition(loginUser.getUserName());
            resultInfo.setData(gridData.getData());
            resultInfo.setNumber(gridData.getNumber());
            resultInfo.setPage(gridData.getPage());
            resultInfo.setTotalPage(gridData.getTotalPage());
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    @RequestMapping("/delMsgInfo")
    @ResponseBody
    private ResultInfo delMsgInfo(String loanId) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
            msgInfoService.delMsgInfo(loanId);
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }

    @RequestMapping("/delMsgInfos")
    @ResponseBody
    private ResultInfo delMsgInfos(String userName) throws Exception {
        ResultInfo resultInfo = new ResultInfo();
        try {
            msgInfoService.delMsgInfoByUser(userName);
            resultInfo.setSuccess("true");
        } catch (Exception e) {
            log.error(e.getMessage());
            resultInfo.setSuccess("false");
            resultInfo.setData(e.getMessage());
        }
        return resultInfo;
    }
}
